<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hotel_City_Model extends CI_Model {

    function __construct() {
        parent::__construct();
    }
	/*
		insert or update city
	*/
	public function save(){
		if(isset($_POST['city_id'])&& $_POST['city_id']=='0'){//insert new hotel
			$sql		= "INSERT INTO hotel_city( ) VALUES( )";
			$query 		= $this->db->query($sql); 
			$city_id	= $this->db->insert_id(); 
			$this->update_city($city_id);
			return array($city_id,'i');   
		}elseif(isset($_POST['city_id'])&& $_POST['city_id']!='0'){//update existing hotel
			$city_id = $_POST['city_id'];
			$this->update_city($city_id);
			return array($city_id,'u');
		}else{
			return 'unknown';
		} 
	}	
	
	/*
		update hotel information
	*/
	private function update_city($city_id=0){
		if($city_id!=0){ 
			$is_enabled = (isset($_POST['is_enabled'])?($_POST['is_enabled']=='1'?1:0):0);
			
			$sql="UPDATE hotel_city SET 
					  name='".mysql_escape_string($_POST['name'])."', 
					  description= '".mysql_escape_string($_POST['description'])."', 
					  latitude='".mysql_escape_string($_POST['latitude'])."', 
					  longitude='".mysql_escape_string($_POST['longitude'])."', 
					  distance_from_colombo='".mysql_escape_string($_POST['distance_from_colombo'])."',
					  distance_from_bia='".mysql_escape_string($_POST['distance_from_bia'])."',
					  is_enabled=".$is_enabled." 
				  WHERE hotel_city.id=".$city_id;  
				 $this->db->query($sql);
			return true;
		}
		return false;
	}
	
	/*
		select hotel details of a given hotel
	*/
	public function load_city_detail($city_id){
		$sql="SELECT 
			  hotel_city.`id`,
			  hotel_city.`name`,
			  hotel_city.description,
			  hotel_city.latitude,
			  hotel_city.longitude,
			  hotel_city.distance_from_colombo,
			  hotel_city.is_enabled,
			  hotel_city.distance_from_bia
			FROM  hotel_city
			WHERE id=".$city_id;
		$query = $this->db->query($sql); 
        return $query->result();
	}	
	/*
		Load city list
	*/
	public function load_city_list(){
			$sql="SELECT 
			  hotel_city.`id`,
			  hotel_city.`name`,  
			  hotel_city.latitude,
			  hotel_city.longitude,
			  hotel_city.distance_from_colombo,
			  hotel_city.is_enabled
			FROM  hotel_city";
		$query = $this->db->query($sql); 
        return $query->result();
	}
	/*
		delete a specified city 
	*/
	public function delete_city($city_id=0){
		if($city_id!='' && $city_id!=0){
			$sql="DELETE FROM hotel_city WHERE hotel_city.id=".$city_id;	
			$this->db->query($sql);
			return true;
		}else{
			return false;	
		}
	}
}